//|||||||||||||||||||||||||||||||||||||||||||||||

#include "GameApp.hpp"
#include "MainMenuState.hpp"
#include "PauseState.hpp"
#include "GameProgressMenuState.hpp"
#include "Game1State.hpp"
#include "Game1FinishedState.hpp"
#include "Game2State.hpp"
#include "Game2FinishedState.hpp"
#include "TipsMenuState.hpp"
#include "TipsMenuState2.hpp"
#include <fstream>

//|||||||||||||||||||||||||||||||||||||||||||||||

GameApp::GameApp()
{
	m_pAppStateManager = 0;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

GameApp::~GameApp()
{
	delete m_pAppStateManager;
    delete OgreFramework::getSingletonPtr();
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameApp::startGame()
{
	new OgreFramework();
	if(!OgreFramework::getSingletonPtr()->initOgre("Stress Control", 0, 0))
		return;

	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Game initialized!");

	m_pAppStateManager = new AppStateManager();

	MainMenuState::create(m_pAppStateManager, "MainMenuState");
	GameProgressMenuState::create(m_pAppStateManager, "GameProgressMenuState");
	Game1State::create(m_pAppStateManager, "Game1State");
	Game1FinishedState::create(m_pAppStateManager, "Game1FinishedState");
	Game2State::create(m_pAppStateManager, "Game2State");
	Game2FinishedState::create(m_pAppStateManager, "Game2FinishedState");
	PauseState::create(m_pAppStateManager, "PauseState");
	TipsMenuState::create(m_pAppStateManager, "TipsMenuState");
	TipsMenuState2::create(m_pAppStateManager, "TipsMenuState2");

	((MainMenuState*)m_pAppStateManager->findByName("MainMenuState"))->setGameApp(this);
	((GameProgressMenuState*)m_pAppStateManager->findByName("GameProgressMenuState"))->setGameApp(this);

	m_pAppStateManager->start(m_pAppStateManager->findByName("MainMenuState"));
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void GameApp::saveGame(GameProgress* progress)
{
	ofstream stream("GameProgress.sv", ios::out);
 
    if(stream)
    {
		progress->writeToFile(&stream);
		stream.close();
	}
	else
	{
		OgreFramework::getSingletonPtr()->m_pLog->logMessage("Impossible to save a game");
	}
	
}
	
//|||||||||||||||||||||||||||||||||||||||||||||||

void GameApp::loadGame(GameProgress* progress)
{
	ifstream stream("GameProgress.sv", ios::in);

	if(stream)
	{
		progress->getFromFile(&stream);
		stream.close();
	}
	else
	{
		OgreFramework::getSingletonPtr()->m_pLog->logMessage("Impossible to load a game");
	}
}